<?php

// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------

namespace base;

/**
 * 微信驱动
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-06-28
 * @desc    支持所有文件存储到硬盘
 */
class Openkongfz {

    // appid
    private $appkey = '27';
    // appsecret
    private $appsecret = "4dfa306de36925f61dad3a60330f9741e8808dc918ce7041f2dd53e63c19c3e9";
    // 
    private $url = "https://open.kongfz.com/router/rest";
  
    private $format = "";
    private $signMethod = "";

    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2017-12-30T18:04:05+0800
     * @param   [string]     $app_id         [应用appid]
     * @param   [string]     $app_secret     [应用密钥]
     */
    public function __construct($appkey,$appsecret, $format = 'json', $signMethod = 'md5') {
        
        $this->appkey = $appkey;
        $this->appsecret = $appsecret;
        $this->format = $format;
        $this->signMethod = $signMethod;
    }
 
 
    /**
     * [查询孔网商品]
     * @author   csb
     */
    public function selectGoods($params) {
        // 请求参数
        $p = [
           
            'appId' => $this->appkey,
            'format' =>  $this->format,
            'v' => '1.0',
            'signMethod' => $this->signMethod ,
            'datetime' => date("Y-m-d H:i:s"),
            'simplify'=>1,
            
        ];
        
        
       
       foreach($params as $k=>$v)
       {
           $p[$k] =$v;
       }
       
    
        $p['sign']= $this->makeSign($p);
      
        
        $res = $this->HttpRequestPost($this->url, $p, true);
        
        return $res;

    }
    
    
    
    // 使用file_get_contents下载图片

    public function dlfile($content, $save_to)
    {
    
        //$content = file_get_contents($file_url);
        
        
        return file_put_contents($save_to, $content);
    
    }
    
    
     /* 生成签名
     * @return 签名，本函数不覆盖sign成员变量，如要设置签名需要调用SetSign方法赋值
     */
    private function makeSign($p)
    {
        //签名步骤一：按字典序排序参数
        ksort($p);
        $string = utf8_encode($this->ToUrlParams($p));
     
    
        $string = md5($this->appsecret.$string.$this->appsecret);
        //签名步骤四：所有字符转为大写
        $result = bin2hex($string);
        return $string;
    }


    /**
     * 格式化参数格式化成url参数
     */
    private function ToUrlParams($p)
    {
        $buff = "";
        foreach ($p as $k => $v)
        {
            if($k != "sign" && !is_array($v)){
                $buff .= $k.$v;
            }
        }
        
        
        return $buff;
    }
    
        
      /**
     * [HttpRequestGet get请求]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-01-03T19:21:38+0800
     * @param    [string]           $url [url地址]
     * @return   [array]                 [返回数据]
     */
    public function HttpRequestGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        
       
        curl_close($curl);
        return json_decode($res, true);
    }

    /**
     * [HttpRequestPost curl模拟post]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2017-09-25T09:10:46+0800
     * @param    [string]   $url        [请求地址]
     * @param    [array]    $data       [发送的post数据]
     * @param    [array]    $is_parsing [是否需要解析数据]
     * @return   [array]                [返回的数据]
     */
    public function HttpRequestPost($url, $data, $is_parsing = true) {

        $headers = array('Content-Type: application/x-www-form-urlencoded');
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $reponse = curl_exec($curl); // 执行操作
    
        if ($is_parsing === true) {
            return json_decode($reponse, true);
        }
        return $reponse;
    }


}

?>